/* eslint-disable no-console */
import { ProfileApi } from '@/entities/profile/api';
import { UpdationChildV2Dto } from '@/shared/interfaces/generated';
import { handleAsyncThunk } from '@/shared/lib/handleThunk';
import { createAction } from '@reduxjs/toolkit';

import { Card } from './reducer';

const ProfileApiService = new ProfileApi();

export const getProfile = handleAsyncThunk('profile/get-profile', ProfileApiService.getProfile);

export const getUniqueAvatarList = handleAsyncThunk('profile/get-avatar', ProfileApiService.getUniqueAvatarList);

export const getCities = handleAsyncThunk(
  'profile/get-cities',
  async ({ search, offset }: { search: string; offset: number }) => {
    const response = await ProfileApiService.getCities(search, 20, offset);
    return response;
  },
);

export const setPagination = createAction('profile/set-pagination');

export const setPremium = createAction('profile/set-premium');

export const addPaymentCard = createAction('profile/add-payment-card', (card: Card) => ({ payload: card }));

export const deletePaymentCard = createAction('profile/delete-payment-card', (card: Card) => ({ payload: card }));

export const setActiveCard = createAction('profile/set-active-card', (card: Card) => ({ payload: card }));

export const clearCities = createAction('profile/clear-cities');

export const removeProfile = handleAsyncThunk('profile/remove-profile', ProfileApiService.removeProfile);

export const editProfile = handleAsyncThunk(
  'profile/edit-profile',
  async (arg: { id: number; profile: Partial<UpdationChildV2Dto> | { name: string | null } }) => {
    const { id, profile } = arg;
    const ans = await ProfileApiService.editProfile(id, profile);
    return ans;
  },
);
